--  --------------------------------------------------------------------------
--  @File:        html_writer.adb
--
--  @Project:     HTML_Writer
--  @Version:     1.2
--  @Created:     2011-11-22
--  @Author:  Benedikt Markt, Aleksander Zotov, Marcel Kranz
--
--  @Description: Creates a HTML file.
--
--  @Modifications:
--    +2011-12-06 BM, AZ, MKR: turned into package;
--                             added Function Initialize;
--                             changed File_Type to HTML_Writer_Type pointer.
--    +2011-12-06 BM, AZ, MKR: Added checkpoints for correct HTML syntax.

with Ada.Text_IO, Ada.Strings.Fixed, Ada.Strings;
use  Ada.Text_IO;

package body HTML_Writer is

   --  -----------------------------------------------------------------------
   --  @Function: Initialize
   function Initialize (File_Name : String) return HTML_Writer_Type is
      File_CR : Ada.Text_IO.File_Type;
      New_Writer : HTML_Writer_Type;
   begin
      New_Writer := new HTML_Writer_Record_Type;
      Ada.Text_IO.Create (New_Writer.File, Ada.Text_IO.Out_File, File_Name);
      return New_Writer;
   end Initialize;

   --  -----------------------------------------------------------------------
   --  @Procedure: Start_Page
   procedure Start_Page (HTML_File : in HTML_Writer_Type) is
   begin
      pragma Assert (HTML_File /= null);
      Ada.Text_IO.Put_Line (HTML_File.File, "<html>");
      HTML_File.State := PAGE_STARTED;
   end Start_Page;

   --  -----------------------------------------------------------------------
   --  @Procedure: Finish_Page
   procedure Finish_Page (HTML_File : in HTML_Writer_Type) is
   begin
      pragma Assert (HTML_File /= null);
      if HTML_File.State /= BODY_FINISHED then
         raise Illegal_State_Exception;
      end if;
      Ada.Text_IO.Put_Line (HTML_File.File, "</html>");
   end Finish_Page;

   --  -----------------------------------------------------------------------
   --  @Procedure: Write_Head
   procedure Write_Head (HTML_File : in HTML_Writer_Type; Title : in String) is
   begin
      pragma Assert (HTML_File /= null);
      if HTML_File.State /= PAGE_STARTED then
         raise Illegal_State_Exception;
      end if;
      Ada.Text_IO.Put (HTML_File.File, "<head><title>");
      Ada.Text_IO.Put (HTML_File.File, Title);
      Ada.Text_IO.Put_Line (HTML_File.File, "</title></head>");
      HTML_File.State := HEAD_WRITTEN;
   end Write_Head;

   --  -----------------------------------------------------------------------
   --  @Procedure: Start_Body
   procedure Start_Body (HTML_File : in HTML_Writer_Type) is
   begin
      pragma Assert (HTML_File /= null);
      if HTML_File.State /= HEAD_WRITTEN then
         raise Illegal_State_Exception;
      end if;
      Ada.Text_IO.Put_Line (HTML_File.File, "<body>");
      HTML_File.State := BODY_STARTED;
   end Start_Body;

   --  -----------------------------------------------------------------------
   --  @Procedure: Finish_Body
   procedure Finish_Body (HTML_File : in HTML_Writer_Type) is
   begin
      pragma Assert (HTML_File /= null);
      if HTML_File.State /= BODY_STARTED then
         raise Illegal_State_Exception;
      end if;
      Ada.Text_IO.Put_Line (HTML_File.File, "</body>");
      HTML_File.State := BODY_FINISHED;
   end Finish_Body;

   --  -----------------------------------------------------------------------
   --  @Procedure: Write_Heading
   procedure Write_Heading
     (HTML_File : in HTML_Writer_Type;
      Text      : in String;
      Level     : in Positive) is
   begin
      pragma Assert (HTML_File /= null);
      if HTML_File.State /= BODY_STARTED then
         raise Illegal_State_Exception;
      end if;
      Ada.Text_IO.Put (HTML_File.File, "<h");
      Ada.Text_IO.Put
        (HTML_File.File,
         Ada.Strings.Fixed.Trim (Positive'Image (Level), Ada.Strings.Both));
      Ada.Text_IO.Put (HTML_File.File, ">");
      Ada.Text_IO.Put (HTML_File.File, Text);
      Ada.Text_IO.Put (HTML_File.File, "</h");
      Ada.Text_IO.Put
        (HTML_File.File,
         Ada.Strings.Fixed.Trim (Positive'Image (Level), Ada.Strings.Both));
      Ada.Text_IO.Put_Line (HTML_File.File, ">");
   end Write_Heading;

   --  -----------------------------------------------------------------------
   --  @Procedure: Add_Paragraph
   procedure Add_Paragraph
     (HTML_File : in HTML_Writer_Type;
      Text : in String) is
   begin
      pragma Assert (HTML_File /= null);
      if HTML_File.State /= BODY_STARTED then
         raise Illegal_State_Exception;
      end if;
      Ada.Text_IO.Put (HTML_File.File, "<p>");
      Ada.Text_IO.Put (HTML_File.File, Text);
      Ada.Text_IO.Put (HTML_File.File, "</p>");
   end Add_Paragraph;

   --  -----------------------------------------------------------------------
   --  @Procedure: Start_List
   procedure Start_List (HTML_File : in HTML_Writer_Type) is
   begin
      pragma Assert (HTML_File /= null);
      if HTML_File.State /= BODY_STARTED then
         raise Illegal_State_Exception;
      end if;
      Ada.Text_IO.Put (HTML_File.File, "<ul>");
      HTML_File.State := LIST_STARTED;
   end Start_List;

   --  -----------------------------------------------------------------------
   --  @Procedure: Finish_List
   procedure Finish_List (HTML_File : in HTML_Writer_Type) is
   begin
      pragma Assert (HTML_File /= null);
      if HTML_File.State /= LIST_STARTED then
         raise Illegal_State_Exception;
      end if;
      Ada.Text_IO.Put (HTML_File.File, "</ul>");
      HTML_File.State := BODY_STARTED;
   end Finish_List;

   --  -----------------------------------------------------------------------
   --  @Procedure: Start_List_Item
   procedure Start_List_Item (HTML_File : in HTML_Writer_Type) is
   begin
      pragma Assert (HTML_File /= null);
      if HTML_File.State /= LIST_STARTED then
         raise Illegal_State_Exception;
      end if;
      Ada.Text_IO.Put (HTML_File.File, "<li>");
      HTML_File.State := ITEM_STARTED;
   end Start_List_Item;

   --  -----------------------------------------------------------------------
   --  @Procedure: Finish_List_Item
   procedure Finish_List_Item (HTML_File : in HTML_Writer_Type) is
   begin
      pragma Assert (HTML_File /= null);
      if HTML_File.State /= ITEM_STARTED then
         raise Illegal_State_Exception;
      end if;
      Ada.Text_IO.Put (HTML_File.File, "</li>");
      HTML_File.State := LIST_STARTED;
   end Finish_List_Item;

   --  -----------------------------------------------------------------------
   --  @Procedure: Add_Link
   procedure Add_Link
     (HTML_File : in HTML_Writer_Type;
      Target    : in String;
      Text      : in String) is
   begin
      pragma Assert (HTML_File /= null);
      if HTML_File.State /= BODY_STARTED
        and HTML_File.State /= ITEM_STARTED then
         raise Illegal_State_Exception;
      end if;
      Ada.Text_IO.Put (HTML_File.File, "<a href =""");
      Ada.Text_IO.Put (HTML_File.File, Target);
      Ada.Text_IO.Put (HTML_File.File, """>");
      Ada.Text_IO.Put (HTML_File.File, Text);
      Ada.Text_IO.Put (HTML_File.File, "</a>");
   end Add_Link;

   --  -----------------------------------------------------------------------
   --  @Procedure: Add_Text
   procedure Add_Text (HTML_File : in HTML_Writer_Type; Text : in String) is
   begin
      pragma Assert (HTML_File /= null);
      if HTML_File.State /= BODY_STARTED
        and HTML_File.State /= ITEM_STARTED then
         raise Illegal_State_Exception;
      end if;
      Ada.Text_IO.Put (HTML_File.File, Text);
   end Add_Text;

end HTML_Writer;
